package com.bz.xtcx.manager.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.mapping.StatementType;

import com.bz.xtcx.manager.entity.LibExpert;
import com.bz.xtcx.manager.mapper.provider.LibExpertProvider;
import com.bz.xtcx.manager.persist.conditions.LibCondition;
import com.bz.xtcx.manager.vo.VoQuery;

public interface LibExpertMapper {

	@Insert("insert into `lib_expert`(id, number, credit_level, form_id, user_id, name, id_number, sex, birthday, education, degree, school, address, country, cellphone, telephone, email,"
			+ "research_field, research_field_other, research_area, work_unit, work_department, job_title, title, title_level, success_record, project_desc, research_winning, project, search_param, status, creater)"
		    + " VALUES(#{id, jdbcType=VARCHAR},"
		    + " #{number, jdbcType=VARCHAR}, #{creditLevel, jdbcType=INTEGER}, #{formId, jdbcType=VARCHAR},"
		    + " #{userId, jdbcType=VARCHAR}, #{name, jdbcType=VARCHAR}, #{code, jdbcType=VARCHAR},"
		    + " #{sex, jdbcType=VARCHAR}, #{birthday, jdbcType=VARCHAR}, #{education, jdbcType=VARCHAR},"
		    + " #{degree, jdbcType=VARCHAR}, #{school, jdbcType=VARCHAR},"
		    + " #{address, jdbcType=VARCHAR}, #{country, jdbcType=VARCHAR},"
		    + " #{cellphone, jdbcType=VARCHAR}, #{telephone, jdbcType=VARCHAR}, #{email, jdbcType=VARCHAR},"
		    + " #{researchField, jdbcType=VARCHAR}, #{researchFieldOther, jdbcType=VARCHAR}, #{researchArea, jdbcType=VARCHAR},"
		    + " #{workUnit, jdbcType=VARCHAR}, #{workDepartment, jdbcType=VARCHAR},"
		    + " #{jobTitle, jdbcType=VARCHAR}, #{title, jdbcType=VARCHAR},"
		    + " #{titleLevel, jdbcType=VARCHAR},  #{successRecord, jdbcType=VARCHAR},"
		    + " #{projectDesc, jdbcType=VARCHAR},  #{researchWinning, jdbcType=VARCHAR},"
		    + " #{project, jdbcType=VARCHAR},"
			+ " #{searchParam, jdbcType=VARCHAR},"
		    + " #{status, jdbcType=INTEGER},"
		    + " #{creater, jdbcType=VARCHAR})"
		    )
    @SelectKey(before = true, keyProperty = "id", resultType = String.class, statementType = StatementType.STATEMENT, statement="select uuid()")
	int insert(LibExpert e);
	
	@Delete("delete from `lib_expert` where id = #{id}")
	int del(String id);
	
	@Update("update `lib_expert` set name=#{name, jdbcType=VARCHAR},id_number=#{code}, credit_level=#{creditLevel, jdbcType=VARCHAR},"
			+ " sex=#{sex, jdbcType=VARCHAR}, birthday=#{birthday, jdbcType=VARCHAR},"
			+ " education=#{education, jdbcType=VARCHAR}, degree=#{degree, jdbcType=VARCHAR}, school=#{school, jdbcType=VARCHAR},"
			+ " address=#{address, jdbcType=VARCHAR}, country=#{country, jdbcType=VARCHAR},"
			+ " cellphone=#{cellphone, jdbcType=VARCHAR}, telephone=#{telephone, jdbcType=VARCHAR}, email=#{email, jdbcType=VARCHAR},"
			+ " research_field=#{researchField, jdbcType=VARCHAR}, research_field_other=#{researchFieldOther, jdbcType=VARCHAR}, research_area=#{researchArea, jdbcType=VARCHAR},"
			+ " work_unit=#{workUnit, jdbcType=VARCHAR},"
			+ " work_department=#{workDepartment, jdbcType=VARCHAR},"
			+ " job_title=#{jobTitle, jdbcType=VARCHAR},"
			+ " title=#{title, jdbcType=VARCHAR},"
			+ " title_level=#{titleLevel, jdbcType=VARCHAR},"
			+ " success_record=#{successRecord, jdbcType=VARCHAR},"
			+ " project_desc=#{projectDesc, jdbcType=VARCHAR},"
			+ " research_winning=#{researchWinning, jdbcType=VARCHAR},"
			+ " project=#{project, jdbcType=VARCHAR},"
            + " search_param=#{searchParam, jdbcType=VARCHAR},"
			+ " status=#{status, jdbcType=INTEGER},"
			+ " updater=#{updater, jdbcType=VARCHAR}"
			+ " where id=#{id}")
	int update(LibExpert e);
	
	@Update("update `lib_expert` set status=#{status, jdbcType=INTEGER} where id=#{id}")
	int updateStatus(@Param("id")String id, @Param("status")int status);
	
	@Update("update `lib_expert` set credit_level=#{creditLevel, jdbcType=INTEGER} where id=#{id}")
	int updateCreditLevel(@Param("id")String id, @Param("creditLevel")int creditLevel);
	
	@SelectProvider(type = LibExpertProvider.class, method = "findByCondition")
	@Results(
		id = "libExpert",
		value = {
			@Result(id = true, property = "id", column = "id"),
			@Result(property = "formId", column = "form_id"),
			@Result(property = "code", column = "id_number"),
		    @Result(property = "form", column = "form_id", one = @One(select = "com.bz.xtcx.manager.mapper.BusUserFormHisMapper.findById") ),
		    @Result(property = "logo", column = "id", one = @One(select = "com.bz.xtcx.manager.mapper.LibImageMapper.findLogoByLibId") ),
            @Result(property = "searchParam", column = "search_param")
	    }
	)
    List<LibExpert> findByCondition(VoQuery e);
	
	@SelectProvider(type = LibExpertProvider.class, method = "findByConditionApi")
    List<LibExpert> findByConditionApi(LibCondition qc);
	
	@Select("select * from `lib_expert` where user_id = #{userId}")
	@ResultMap("libExpert")
	LibExpert findByUserId(String userId);
	
	@Select("select * from `lib_expert` where name = #{name} and id_number = #{code} limit 1")
	@ResultMap("libExpert")
	LibExpert findByNameAndCode(@Param("name")String name, @Param("code")String code);
	
	@Select("select * from `lib_expert` where id = #{id}")
	@ResultMap("libExpert")
	LibExpert findById(String id);
	
	@Select("select * from `lib_expert` where form_id = #{id}")
	@ResultMap("libExpert")
	LibExpert findByFormId(String id);
}
